{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Excel Sheet Column Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: titleToNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #Excel 表列序号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串&nbsp;<code>columnTitle</code> ，表示 Excel 表格中的列名称。返回 <em>该列名称对应的列序号</em>&nbsp;。</p>\n",
    "\n",
    "<p>例如：</p>\n",
    "\n",
    "<pre>\n",
    "A -&gt; 1\n",
    "B -&gt; 2\n",
    "C -&gt; 3\n",
    "...\n",
    "Z -&gt; 26\n",
    "AA -&gt; 27\n",
    "AB -&gt; 28 \n",
    "...</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> columnTitle = \"A\"\n",
    "<strong>输出:</strong> 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例&nbsp;2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>columnTitle = \"AB\"\n",
    "<strong>输出:</strong> 28\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例&nbsp;3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>columnTitle = \"ZY\"\n",
    "<strong>输出:</strong> 701</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= columnTitle.length &lt;= 7</code></li>\n",
    "\t<li><code>columnTitle</code> 仅由大写英文组成</li>\n",
    "\t<li><code>columnTitle</code> 在范围 <code>[\"A\", \"FXSHRXW\"]</code> 内</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [excel-sheet-column-number](https://leetcode.cn/problems/excel-sheet-column-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [excel-sheet-column-number](https://leetcode.cn/problems/excel-sheet-column-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"A\"', '\"AB\"', '\"ZY\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        result = 0\n",
    "        title = list(s)[::-1]\n",
    "        for index, chr in enumerate(title):\n",
    "            val = pow(26, index) * (int((ord(chr) - ord('A')) + 1))\n",
    "            result += val\n",
    "            \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        res = 0\n",
    "        for i, c in enumerate(s[::-1]):\n",
    "            res += (ord(c) - ord(\"A\") + 1) * 26**i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        res = 0\n",
    "        for c in s:\n",
    "            res *= 26\n",
    "            res += ord(c) - ord('A') + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        r=\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n",
    "        shuju=0\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            shuju+=(r.find(s[i])+1)*26**(len(s)-1-i)\n",
    "            print(shuju)\n",
    "        return(shuju)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        r = 0\n",
    "        for c in s.encode('ascii'):\n",
    "            print(c)\n",
    "            r *= 26\n",
    "            r += c - 64\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        result = 0\n",
    "        for i in s:\n",
    "            result = result * 26 + ord(i) - 64\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        n = len(s)\n",
    "        digit = 0\n",
    "        i = n - 1\n",
    "        ret = 0\n",
    "        while i >= 0:\n",
    "            num = ord(s[i]) - 64\n",
    "            ret = num * (26 ** digit) + ret\n",
    "            digit += 1\n",
    "            i -= 1\n",
    "            print(s[i],num, digit, ret)\n",
    "\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        l = len(s)\n",
    "        total = 0\n",
    "        for item in s:\n",
    "            total += (ord(item) - 64) * 26 **(l-1)\n",
    "            l -= 1\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        iSum = 0\n",
    "        for c in s:\n",
    "            iSum = iSum*26 + ord(c) - 64\n",
    "        return iSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        #用的chr()把ASCII码转化为对应的字符，把字母和数字建立字典对应表\n",
    "        #不过用ord()更方便\n",
    "        dict1={}\n",
    "        sum=0\n",
    "        for i in range(97,123):\n",
    "            dict1[chr(i).upper()]=i-96\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            sum+=dict1[s[i]]*26**(len(s)-1-i)\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "\n",
    "        alp = {}\n",
    "        strs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n",
    "        for i in range(len(strs)):\n",
    "            alp[strs[i]] = i+1\n",
    "        \n",
    "        ans,t = 0,1\n",
    "        for i in reversed(range(len(s))):\n",
    "            ans += alp[s[i]] * t\n",
    "            t *= 26\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        sums=0\n",
    "        s=s[::-1]\n",
    "        for i in range(len(s)):\n",
    "            val=ord(s[i])-64\n",
    "            sums+=val*(26**i)\n",
    "        return sums\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        record = {'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8,'I':9,\n",
    "                  'J':10,'K':11,'L':12,'M':13,'N':14,'O':15,'P':16,'Q':17,\n",
    "                  'R':18,'S':19,'T':20,'U':21,'V':22,'W':23,'X':24,'Y':25,'Z':26}\n",
    "        result = 0\n",
    "        for i, c in enumerate(s):\n",
    "            result += record[c]*pow(26,len(s)-i-1)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(s)):\n",
    "            ans += (ord(s[i]) - 64) * 26 ** (len(s) - 1 - i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        ans=0\n",
    "        for i in s:\n",
    "            ans=ans*26\n",
    "            ans+=ord(i)-64\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        def f(st):\n",
    "            return ord(st) - 64\n",
    "\n",
    "        length = len(s)\n",
    "        e = 1\n",
    "        res = 0\n",
    "        for i in range(length - 1, -1, -1):\n",
    "            res += f(s[i]) * e\n",
    "            e *= 26\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        \n",
    "        for letter in s:\n",
    "            num = ord(letter) - ord('A') + 1\n",
    "            ans  = ans * 26 + num\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        check = [chr(i) for i in range(65, 91)]\n",
    "        sl=list(s)\n",
    "        sl.reverse()\n",
    "        r=0\n",
    "        for i in range(len(sl)):\n",
    "            r+=(1+check.index(sl[i]))* (26**i)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "\n",
    "        \"\"\"\n",
    "        4/2 0,0,1\n",
    "        \n",
    "        AB\n",
    "        AAB\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "        n = len(s)\n",
    "        total = 0\n",
    "        for i in range(n):\n",
    "            pos = n - i - 1\n",
    "            print(i)\n",
    "            print((26 ** i))\n",
    "            # print(ord(s[pos]), ord('A'))\n",
    "            total += ((ord(s[pos]) - ord('A') + 1) * (26 ** i))\n",
    "        return total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        result = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            result += (ord(s[-1-i])-64)*(26**i)\n",
    "            print(s[i],ord(s[-1-i])-64,i,26**i,result)\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, s: str) -> int:\n",
    "        #print(ord('z')-ord('a'))\n",
    "        num = 0\n",
    "        for i in range(0,len(s)):\n",
    "            print(num)\n",
    "            num += num*25 + (ord(s[i])-ord('A')+1)\n",
    "            print(num,(ord(s[i])-ord('A')+1))\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        res = 0\n",
    "        for i in columnTitle:\n",
    "            res = res * 26 + ord(i)- 64\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        result = 0 \n",
    "        n = 1\n",
    "        for i in range(len(columnTitle)-1,-1,-1):\n",
    "            a = ord(columnTitle[i]) - ord('A') + 1\n",
    "            result += a*n \n",
    "            n = n*26\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        res = 0\n",
    "        for i in columnTitle:\n",
    "            res *= 26\n",
    "            res += ord(i) - ord('A') + 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        ans = 0\n",
    "        for i in columnTitle:\n",
    "            ans = ans*26 + ord(i) - 64\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        ret = 0\n",
    "        for idx, ch in enumerate(columnTitle[::-1]):\n",
    "            ret += (ord(ch)- ord('A') + 1) * 26**idx\n",
    "            print(ord(ch))\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        # 获取单个字母对应的列序号\n",
    "        def single_word(word):\n",
    "            return ord(word) - 64\n",
    "\n",
    "\n",
    "        # 计算给定字符所对应的列序号\n",
    "        def set_nu(s):\n",
    "            temp = map(single_word, s)\n",
    "            res = 0\n",
    "            handle_list = list(temp)\n",
    "            length = len(handle_list)\n",
    "            print(length)\n",
    "            for i, j in enumerate(handle_list):\n",
    "                print(j, i)\n",
    "                if i < length - 1:\n",
    "                    print(j)\n",
    "                    res += j * 26 ** (length - i - 1)\n",
    "            return res + handle_list[-1]\n",
    "        return set_nu(columnTitle)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        ret  = 0\n",
    "        for i in columnTitle:\n",
    "            ret = ret*26 + ord(i) - 64\n",
    "        return ret "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        result = 0\n",
    "        for char in columnTitle:\n",
    "            result = result * 26 + ord(char) - ord('A') + 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def titleToNumber(self, columnTitle: str) -> int:\n",
    "        s=columnTitle[::-1]\n",
    "        sum=0\n",
    "        for i in range(len(columnTitle)):\n",
    "            sum=sum+(ord(s[i])-64)*26**(i)\n",
    "        return int(sum)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
